package com.jswlw.service.base.impl;

import com.jswlw.dao.base.RoleMapper;
import com.jswlw.dao.base.RoleMenuMapper;
import com.jswlw.entity.base.Role;
import com.jswlw.entity.base.RoleMenu;
import com.jswlw.service.base.RoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wu on 2017-12-07.
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Resource
    RoleMapper roleMapper;

    @Resource
    RoleMenuMapper roleMenuMapper;

    public List<Role> findAll(Map condition) {
        return roleMapper.findAll(condition);
    }

    public Role findOne(Integer id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    public int insert(Role role) {
        return roleMapper.insertSelective(role);
    }

    public int update(Role role) {
        return roleMapper.updateByPrimaryKeySelective(role);
    }

    public int delete(Integer id) {
        return roleMapper.deleteByPrimaryKey(id);
    }

    public int findCount(Map map) {
        return roleMapper.findCount(map);
    }

    public List<Role> searchPage(Map map) {
        return null;
    }

    @Override
    public List<RoleMenu> findAuth4Role(Integer roleId) {
        Map condition = new HashMap();
        condition.put("roleId", roleId);
        return roleMenuMapper.findAll(condition);
    }

    @Override
    public void saveAuth(Integer[] menus, Integer roleId) {
        roleMenuMapper.delete4Role(roleId);
        if (menus != null) {
            for(Integer menu:menus){
                RoleMenu roleMenu=new RoleMenu();
                roleMenu.setRoleId(roleId);
                roleMenu.setMenuId(menu);
                roleMenuMapper.insertSelective(roleMenu);
            }
        }
    }


}
